$import("biz.base.CompositePanel"); 
$import('biz.ext3.examples.ux.TabCloseMenu');
$import('biz.ext3.examples.ux.TabScrollerMenu');
var companel = new CompositePanel('BT_UNIT');

function initOrgTree(){
	var treeLoader = new Ext.tree.TreeLoader({
		dataUrl : '../module/Unit-getUnitTree.action'
	});
	var tree = new Ext.tree.TreePanel({
		id : 'org_tree',
		collapsible:true,
		title : '机构组织',
		split : true,
		region : 'west',
		autoScroll : true,//解决列表过长无法滚动的问题
		width : 400,
		animate : true,
		floatable : false,//仅点击箭头才能伸缩(无临时弹出):
		titleCollapse : true,
		margins : '0 0 0 1',
		rootVisible : false,
		lines : false,
		useArrows : true,
		loader : treeLoader,
		root : new Ext.tree.AsyncTreeNode({
			id : unid,
			text : '根节点'
		}),
		tbar:[{
			text:"刷新",iconCls:"icon-reload",
			handler:function(){
				tree.getRootNode().reload();
			}
		}],
		bbar:[{
			text:"新建子级",iconCls:'icon-create',
			handler:function(){
				var node=tree["cur_node"];
				if(!node){
					tree["cur_node"]=tree.getRootNode();
					node=tree["cur_node"];
				}
				var url=getContextBase()+"module/Unit-createJSON.action?nid="+companel.getMetaId()+"&dataTO.map.MID="+node.attributes["id"];
				doHttpPost(url);
				tree.getRootNode().reload();
			}
		},{
			text:"删除节点",
			iconCls:'icon-delete',
			handler:function(){
				var node=tree["cur_node"];
				if(!node){
					return Ext.Msg.alert('提示',"请先选择一个节点");
				}
				var attrs=node.attributes;
				Ext.Msg.confirm('请确认!','确定要删除此节点？',function(){
					var url = getContextBase() +"module/Mace-deleteRolesByIds.action?jsonStr="+Ext.encode([attrs["id"]]);
					doHttpPost(url);
					tree.getRootNode().reload();
					tree.expandAll();
					tree["cur_node"]=null;
				});
			}
		}]
	});
	
	tree.on('beforeload',function(node){          
		tree.loader.dataUrl ='../module/Unit-getUnitTree.action?sid='+node.id;  
    });
	
	tree.on('click',function(n){
		tree["cur_node"]=n;
	});
	return tree;
}



function initDetailForm(){
	var form = new Ext.form.FormPanel({
		title : '机构组织信息',
		split : true,
		id:'detail_form',
		collapsible : true,
		region : 'center',
		frame : true,
		height : 200,
		width:400,
		labelWidth : 70,
		labelAlign : 'right',
		layout : 'form',
		buttonAlign:'left',
		buttons:[{
			xtype : 'button',
			iconCls:'icon-submit',
			text:'保存',
			handler:function(){
				var tree=Ext.getCmp('org_tree');
				 var conf={
				  		    clientValidation: true,
				  		    url: getContextBase()+'module/Record-saveJSON.action?nid='+companel.getMetaId()+'&sid='+tree["cur_node"]["attributes"]["id"],
				  		    success: function(form, action) {
				  		    	tree.getRootNode().reload();
				  		    	tree.expandAll();
				     		},
				  		    failure: function(form, action) {
				  		        switch (action.failureType) {
				  		            case Ext.form.Action.CLIENT_INVALID:
				  		                Ext.Msg.alert('失败', '表单验证失败。');
				  		                break;
				  		            case Ext.form.Action.CONNECT_FAILURE:
				  		                Ext.Msg.alert('失败', '异步提交失败。');
				  		                break;
				  		            case Ext.form.Action.SERVER_INVALID:
				  		               Ext.Msg.alert('失败', action.result.msg);
				  		       }
				  		    }
				  		};
				 form.getForm().submit(conf);
			}
		}],
		items :companel.loadFormFields()
	});
	return form;
}

Ext.onReady(function() {
	Ext.QuickTips.init();
	var orgTree = initOrgTree();
	var detailForm = initDetailForm();
	var panel = new Ext.Viewport({
			width : 900,
			height : document.documentElement.clientHeight - 10,
			renderTo : 'container4',
			layout : 'border',
			items : [orgTree,detailForm]
		});
});
//加载当前选中机构的详细信息到"机构信息"面板
function loadOrgDetail(){
	var n=Ext.getCmp('org_tree')["cur_node"];
	if(!n["detail"]){
		var url=getContextBase()+"module/Record-query.action?nid="+companel.getMetaId()+"&filter.SID="+n.attributes["id"];
		n["detail"] =Ext.decode( doHttpPost(url).text)["itemList"][0];
	}
	var form=Ext.getCmp('detail_form');
	form.items.each(function(item,index,length){
		var name=item.getName();
		name=name.substring(name.lastIndexOf('.')+1);
		item.setValue(n["detail"][name]);
		item.originalValue=n["detail"][name];
	});
};



